﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Security.Tokens;
using System.Threading;
using Pixysoft.TestDrivens;

namespace Pixysoft.Framework.Security
{
    class TC_3000
    {
        /// <summary>
        /// ！！！！使用前必读！！！！！
        /// </summary>
        public void test001()
        {
            //TokenSecurityManager，使用的是基于了SQLite数据库的权限验证

            //初始化的时候，使用了默认的用户 helloworld / helloworld，使用了默认数据库 helloworld.db

            // 因此初始化后，要立刻删除数据库中的默认用户。

            //相关配置在TokenHelper.cs文件里面。

            Console.WriteLine(Pixysoft.Security.MD5.GetMD5("helloworld"));
        }


        //all test
        public void test002()
        {
            TokenSecurityManager.Instance.Restart();

            string token = TokenSecurityManager.Instance.Login("helloworld", Pixysoft.Security.MD5.GetMD5("helloworld"));

            //---------

            Console.WriteLine(token);

            //---------

            Assert.IsTrue(TokenSecurityManager.Instance.Register(token, "test1", "test1"));

            TokenSecurityManager.Instance.TokensPurge("helloworld", "helloworld");

            //---------

            Assert.IsTrue(TokenSecurityManager.Instance.TokenValidate(token));

            token = TokenSecurityManager.Instance.Login("test1", "test1");

            TokenSecurityManager.Instance.Logout(token);

            //---------

            Assert.IsFalse(TokenSecurityManager.Instance.TokenValidate(token));

            Assert.IsFalse(TokenSecurityManager.Instance.IsAnonymous(token));

            token = TokenSecurityManager.Instance.Login("helloworld", Pixysoft.Security.MD5.GetMD5("helloworld"));

            Assert.IsTrue(TokenSecurityManager.Instance.Cancellation(token, "test1", "test1"));

            token = TokenSecurityManager.Instance.Login("test1", "test1");

            Assert.IsNull(token);


            token = TokenSecurityManager.Instance.AnonymousLogin();

            Assert.IsNotNull(token);

            Assert.IsTrue(TokenSecurityManager.Instance.IsAnonymous(token));

            Assert.IsTrue(TokenSecurityManager.Instance.TokenValidate(token));

            Assert.IsFalse(TokenSecurityManager.Instance.TokenValidate(token));

            TokenSecurityManager.Instance.Logout(token);
        }

        //generate user.config
        public void test003()
        {
            List<TokenUserInfo> list = new List<TokenUserInfo>();
            TokenUserInfo profile = new TokenUserInfo("test1", "test1");
            list.Add(profile);

            Console.WriteLine(Pixysoft.IO.XmlExporter.Instance.XmlSerialize(list));
        }

        //shutdown
        public void test004()
        {
            TokenSecurityManager.Instance.Restart();

            string pwd = Pixysoft.Security.MD5.GetMD5("helloworld");

            string token = TokenSecurityManager.Instance.Login("helloworld", pwd);

            TokenSecurityManager.Instance.Reload(token);

            TokenSecurityManager.Instance.ShutDown(token);

            token = TokenSecurityManager.Instance.Login("helloworld", pwd);

            Console.WriteLine(token);
        }

        //server start
        public void test005()
        {
            TokenSecurityManager.Instance.Restart();

            string pwd = Pixysoft.Security.MD5.GetMD5("helloworld");

            string token = TokenSecurityManager.Instance.Login("helloworld", pwd);

            TokenSecurityManager.Instance.Reload(token);

            TokenSecurityManager.Instance.Logout(token);

        }

        //anonymous
        public void test006()
        {
            string token = TokenSecurityManager.Instance.AnonymousLogin();

            Console.WriteLine(TokenSecurityManager.Instance.TokenValidate(token));

            Console.WriteLine(TokenSecurityManager.Instance.TokenValidate(token));
        }

        //timeout
        public void test007()
        {
            string pwd = Pixysoft.Security.MD5.GetMD5("helloworld");

            string token = TokenSecurityManager.Instance.Login("helloworld", pwd);

            Console.WriteLine(TokenSecurityManager.Instance.TokenValidate(token));

            Thread.Sleep(1000);

            Console.WriteLine(TokenSecurityManager.Instance.TokenValidate(token));

            Thread.Sleep(5000);

            Console.WriteLine(TokenSecurityManager.Instance.TokenValidate(token));

            Console.WriteLine(TokenSecurityManager.Instance.TokenValidate(token));


        }

    }
}
